#define MSUSER_CONTROL_C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//user header
#include <libmscommon/mscommon.h>
#include <libmscommon/mscjson.h>
#include <libmscommon/msmd.h>
#include <libmscommon/msstring.h>
#include <libmslog/mslog.h>
#include "msweb.h"

#define FLAG "USE_CONTROL"
#define msdb_msusercontrol  		"/etc/mscore/db/msdb_msusercontrol.db"

typedef struct MSUSERCONTROLContext{
	ms_bool init;
	ms_u08  mode;		//0-disable,1-blacklist and whitelst
	ms_u32  num_blacklist;	
	ms_u32  num_whitelist;
	ms_u32  num_whitelst_stateserver;
	ms_u32  num_whitelst_mssdkapi;
	ms_byte listipaddr_blackl[256][128];
	ms_byte listipaddr_white[256][128];
	ms_byte listipaddr_white_mssdkapi[256][128];
	ms_byte listipaddr_white_stateserver[256][128];
}MSUSERCONTROLContext;

static MSUSERCONTROLContext msusrctl_ctt;

ms_s08 msmsusrctl_api_init(ms_void)
{
	MSUSERCONTROLContext *pmsusrctl_ctt=&msusrctl_ctt;
	ms_stru0(pmsusrctl_ctt, MSUSERCONTROLContext)
	ms_byte json_buf[40960]={0};
	if(0>msmd_api_read2(msdb_msusercontrol, json_buf, sizeof(json_buf))){
		pmsusrctl_ctt->init=ms_false;
		ms_errRet(-1,"msmd_api_read2 (%s)faild",msdb_msusercontrol);
	}
	
	cJSON *jsonroot = cJSON_Parse(json_buf);
	if(!jsonroot) {
		ms_errRet(-1,"cJSON_Parse:get root faild");
	}
	cJSON *js_item=ms_null;
	cJSON *js_array=ms_null;
	cJSON *js_list=ms_null;
	MSJSON_PARSE_INT(js_item, jsonroot, "mode", pmsusrctl_ctt->mode);

	
	js_array=cJSON_GetObjectItem(jsonroot,"msblacklist");
	if(ms_null!=js_array){
		int arrySize=cJSON_GetArraySize(js_array); 
		js_list=js_array->child;
		int ret=0;
		while(js_list!=ms_null)  {  
			MSJSON_PARSE_STRING(js_item, js_list, "ipaddr", pmsusrctl_ctt->listipaddr_blackl[pmsusrctl_ctt->num_blacklist]);	
			pmsusrctl_ctt->num_blacklist++;
			js_list=js_list->next; 
		}  
	}

	js_array=cJSON_GetObjectItem(jsonroot,"mswhitelist");
	if(ms_null!=js_array){
		int arrySize=cJSON_GetArraySize(js_array); 
		js_list=js_array->child;
		int ret=0;
		while(js_list!=ms_null)  {  
			MSJSON_PARSE_STRING(js_item, js_list, "ipaddr", pmsusrctl_ctt->listipaddr_white[pmsusrctl_ctt->num_whitelist]);	
			pmsusrctl_ctt->num_whitelist++;
			js_list=js_list->next; 
		}  
	}

	js_array=cJSON_GetObjectItem(jsonroot,"mswhitelst_stateserver");
	if(ms_null!=js_array){
		int arrySize=cJSON_GetArraySize(js_array); 
		js_list=js_array->child;
		int ret=0;
		while(js_list!=ms_null)  {  
			MSJSON_PARSE_STRING(js_item, js_list, "ipaddr", pmsusrctl_ctt->listipaddr_white_stateserver[pmsusrctl_ctt->num_whitelst_stateserver]);	
			pmsusrctl_ctt->num_whitelst_stateserver++;
			js_list=js_list->next; 
		}  
	}

	js_array=cJSON_GetObjectItem(jsonroot,"mswhitelst_mssdkapi");
	if(ms_null!=js_array){
		int arrySize=cJSON_GetArraySize(js_array); 
		js_list=js_array->child;
		int ret=0;
		while(js_list!=ms_null)  {  
			MSJSON_PARSE_STRING(js_item, js_list, "ipaddr", pmsusrctl_ctt->listipaddr_white_mssdkapi[pmsusrctl_ctt->num_whitelst_mssdkapi]);	
			pmsusrctl_ctt->num_whitelst_mssdkapi++;
			js_list=js_list->next; 
		}  
	}

	pmsusrctl_ctt->init=ms_true;
	return 0;
}
void msmsusrctl_api_unAuthorized(char *outbuf,ms_bool flag_retlogin)
{
char tempbuf[2048]={0};
	ms_strcats(tempbuf,outbuf,"<!DOCTYPE html>");
	ms_strcats(tempbuf,outbuf,"<html><head>");
	ms_strcats(tempbuf,outbuf,"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
	ms_strcats(tempbuf,outbuf,"<title>Not Found</title>");
	ms_strcats(tempbuf,outbuf,"<style>");
	ms_strcats(tempbuf,outbuf,"body {");
	ms_strcats(tempbuf,outbuf,"width: 110em;");
	ms_strcats(tempbuf,outbuf,"margin: 0 auto;");
	ms_strcats(tempbuf,outbuf,"font-family: Tahoma, Verdana, Arial, sans-serif;");
	ms_strcats(tempbuf,outbuf,"}");
	ms_strcats(tempbuf,outbuf,"</style>");
	ms_strcats(tempbuf,outbuf,"</head>");
	ms_strcats(tempbuf,outbuf,"<body>");
	ms_strcats(tempbuf,outbuf,"<h1 "MSWEB_H1_STYLE">403,Unauthorized access</h1>");
	if(ms_true==flag_retlogin){	
		ms_strcats(tempbuf,outbuf,"<h1><a href=\"/%s/\" "MSWEB_FONT_STYLE"<b>%s</b></a></h1>","mscore","返回登录|LOGIN");		
	}
	ms_strcats(tempbuf,outbuf,"</body></html>");
}

ms_bool msmsusrctl_api_isBlack(ms_string ipaddr)
{
	ms_pamcheckRet(ms_false, ipaddr, "ipaddr");
	MSUSERCONTROLContext *pmsusrctl_ctt=&msusrctl_ctt;
	if(ms_false==pmsusrctl_ctt->init){
		return ms_false;
	}
	ms_u08   index=0;
	for(index=0;index<pmsusrctl_ctt->num_blacklist;index++){
		//ms_debug("%s-%s",pmsusrctl_ctt->blacklistip[index], ipaddr);
		if(ms_strncmp_saeq(pmsusrctl_ctt->listipaddr_blackl[index], ipaddr)){
			return ms_true;
		}
	}
	return ms_false;
}
ms_bool msmsusrctl_api_isWhite(ms_string ipaddr)
{
	ms_pamcheckRet(ms_false, ipaddr, "ipaddr");
	MSUSERCONTROLContext *pmsusrctl_ctt=&msusrctl_ctt;
	if(ms_false==pmsusrctl_ctt->init){
		return ms_false;
	}
	ms_u08   index=0;
	for(index=0;index<pmsusrctl_ctt->num_whitelist;index++){
		//ms_debug("%s-%s",pmsusrctl_ctt->blacklistip[index], ipaddr);
		if(ms_strncmp_saeq(pmsusrctl_ctt->listipaddr_white[index], ipaddr)){
			return ms_true;
		}
	}
	return ms_false;
}
ms_bool msmsusrctl_api_isWhite_stateserver(ms_string ipaddr)
{
	ms_pamcheckRet(ms_false, ipaddr, "ipaddr");
	MSUSERCONTROLContext *pmsusrctl_ctt=&msusrctl_ctt;
	if((ms_false==pmsusrctl_ctt->init)||(0==ms_buflen(ipaddr))||(0==pmsusrctl_ctt->num_whitelst_stateserver)){
		return ms_true;
	}
	ms_u08   index=0;
	for(index=0;index<pmsusrctl_ctt->num_whitelst_stateserver;index++){
		if(msstr_api_isCasestr(ipaddr,pmsusrctl_ctt->listipaddr_white_stateserver[index])){
			return ms_true;
		}
	}
	return ms_false;
}
ms_bool msmsusrctl_api_isWhite_mssdkapi(ms_string ipaddr)
{
	ms_pamcheckRet(ms_false, ipaddr, "ipaddr");
	MSUSERCONTROLContext *pmsusrctl_ctt=&msusrctl_ctt;
	if((ms_false==pmsusrctl_ctt->init)||(0==ms_buflen(ipaddr))||(0==pmsusrctl_ctt->num_whitelst_mssdkapi)){
		return ms_true;
	}
	ms_u08   index=0;
	for(index=0;index<pmsusrctl_ctt->num_whitelst_mssdkapi;index++){
		if(msstr_api_isCasestr(ipaddr,pmsusrctl_ctt->listipaddr_white_mssdkapi[index])){
			return ms_true;
		}
	}
	return ms_false;
}

ms_void msmsusrctl_api_webinfo(ms_string ms_out outbuf)
{
	MSUSERCONTROLContext *pmsusrctl_ctt=&msusrctl_ctt;
	char tempbuf[1024]={0};
	char opttemp[1024]={0};
	char optstr[1024]={0};	
	if(ms_false==pmsusrctl_ctt->init){
		ms_strcats(tempbuf,outbuf,"<h4 "MSWEB_FONT_STYLE">Wait for  msusrctl_ctt init...</h4>") ;
	}else	{
		if(1!=pmsusrctl_ctt->mode){
			ms_strcats(tempbuf,outbuf,"<h4 "MSWEB_FONT_STYLE">Disable...</h4>") ;
		}else{
			ms_strcats(tempbuf,outbuf,
				"<table cellpadding=\"4\" cellspacing=\"0\" "MSWEB_FONT_STYLE">");
			ms_strcats(tempbuf,outbuf,
				"<tbody>"
			);
			ms_u32 index_item=0;
			ms_u32 index=0;
			for(index=0;index<pmsusrctl_ctt->num_blacklist;index++){
				ms_strcats(tempbuf,outbuf,"%s","<tr>");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%03d</th>""<th align=\"left\"></th>",(++index_item));
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>","black");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>",pmsusrctl_ctt->listipaddr_blackl[index]);
				ms_strcats(tempbuf,outbuf,"%s","</tr>");
			}
			for(index=0;index<pmsusrctl_ctt->num_whitelist;index++){
				ms_strcats(tempbuf,outbuf,"%s","<tr>");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%03d</th>""<th align=\"left\"></th>",(++index_item));
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>","white");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>",pmsusrctl_ctt->listipaddr_white[index]);
				ms_strcats(tempbuf,outbuf,"%s","</tr>");
			}
			for(index=0;index<pmsusrctl_ctt->num_whitelst_stateserver;index++){
				ms_strcats(tempbuf,outbuf,"%s","<tr>");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%03d</th>""<th align=\"left\"></th>",(++index_item));
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>","white_stateserver");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>",pmsusrctl_ctt->listipaddr_white_stateserver[index]);
				ms_strcats(tempbuf,outbuf,"%s","</tr>");
			}
			for(index=0;index<pmsusrctl_ctt->num_whitelst_mssdkapi;index++){
				ms_strcats(tempbuf,outbuf,"%s","<tr>");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%03d</th>""<th align=\"left\"></th>",(++index_item));
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>","white_mssdkapi");
				ms_strcats(tempbuf,outbuf,"<th align=\"left\">""%s</th>""<th align=\"left\"></th>",pmsusrctl_ctt->listipaddr_white_mssdkapi[index]);
				ms_strcats(tempbuf,outbuf,"%s","</tr>");
			}

			ms_strcats(tempbuf,outbuf,"%s","</tr>");
		}
	}	 
	ms_strcats(tempbuf,outbuf ,	
		"</tbody>"
		);			
	ms_strcats(tempbuf,outbuf,	
		"</table>"
		);
}

#undef MSUSER_CONTROL_C

